package com.link.base.base.product.service;

import com.link.base.base.accnt.model.Account;
import com.link.base.base.accnt.service.AccountService;
import com.link.base.base.cmqres.model.CmqRes;
import com.link.base.base.cmqres.service.CmqResService;
import com.link.base.base.product.dao.mybatis.mapper.PriceListMapper;
import com.link.base.base.product.model.PriceList;
import com.link.base.base.orgnization.model.Orgnization;
import com.link.base.base.orgnization.service.OrgnizationService;
import com.link.core.basic.dao.mapper.BasicMapper;
import com.link.core.basic.service.ServiceException;
import com.link.core.basic.service.BasicServiceImpl;
import com.link.core.mq.util.MqUtil;
import com.link.core.cllog.LCLogger;
import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author unknown
 * @date unknown
 * @comments 价格表行项目服务
 */
@Service
public class PriceListServiceImpl extends BasicServiceImpl<PriceList> implements PriceListService {

    @Resource
    private PriceListMapper priceListMapper;
    @Resource
    private CmqResService cmqResService;
    @Resource
    private AccountService accountService;
    @Resource
    private OrgnizationService orgnizationService;

    @Override
    public BasicMapper<PriceList> getBasicMapper() throws Exception {
        return priceListMapper;
    }

    @Override
    public void afterUpdate(PriceList priceList) throws Exception {
        CmqRes cmqRes = new CmqRes();
        List<CmqRes> cmqResList = cmqResService.queryAll(cmqRes);
        if (cmqResList.size() > 0) {
            try {
                String message1 = "PriceTableSyncl;{\"action\":\"Update\",\"objectId\":\"";
                String message2 = "\"}";
                String message = message1 + priceList.getId() + message2;
                String topicName = cmqResList.get(0).getQueueName();
                MqUtil.sendCmqTopic(topicName, message);
            } catch (Exception e) {
                LCLogger.withException(e);
            }
        }
    }

    @Override
    public void afterInsert(PriceList priceList) throws Exception {
        CmqRes cmqRes = new CmqRes();
        List<CmqRes> cmqResList = cmqResService.queryAll(cmqRes);
        if (cmqResList.size() > 0) {
            try {
                String message1 = "PriceTableSyncl;{\"action\":\"Insert\",\"objectId\":\"";
                String message2 = "\"}";
                String message = message1 + priceList.getId() + message2;
                String topicName = cmqResList.get(0).getQueueName();
                MqUtil.sendCmqTopic(topicName, message);
            } catch (Exception e) {
                LCLogger.withException(e);
            }
        }
    }

    @Override
    public PriceList queryDealerSalePriceHead(Long acctId) throws Exception {
        if (acctId == null) {
            throw new ServiceException("PRODUCT-009");
        }
        Account account = accountService.queryById(acctId);
        if (account == null) {
            throw new ServiceException("PRODUCT-010");
        }

        Long orgId = acctId;
        //校验组织
        Orgnization org = orgnizationService.queryById(orgId);
        if (org == null) {
            throw new ServiceException("PRODUCT-011");
        }
        Long priceListId = org.getPriceListId();
        if (priceListId == null) {
            throw new ServiceException("PRODUCT-012");
        }
        PriceList priceEntity = this.queryById(priceListId);
        return priceEntity;
    }
}

